<script setup lang="ts">
	import {
		reactive, ref
	} from 'vue'
	import {
		onShow, onLoad, onShareAppMessage
	} from '@dcloudio/uni-app'
	import loginPop from '@/components/loginPop.vue'
	import user from '../../api/user.js';
	import common from '../../api/common.js';
	import urls from '../../api/baseUrl.js';
	import share from '../../utils/share.js'
	let parames: any = reactive({
		banner: { content: [''] },
		list: [
			{
				title: '基地订单',
				id: 0,
				path: '/pages/hotel/orderList',
				icon: 'icon-right',
				size: '31rpx',
				isToken: false
			},
			{
				title: '活动订单',
				id: 1,
				path: '/pages/me/activityOrders',
				icon: 'icon-right',
				size: '31rpx',
				isToken: false
			},
			{
				title: '钱包',
				id: 2,
				path: '/pages/me/wallet',
				icon: 'icon-right',
				size: '31rpx',
				isToken: true
			},
			{
				title: '代理商',
				id: 3,
				path: '/pages/me/agent/index',
				icon: 'icon-right',
				size: '31rpx',
				isToken: true
			},
			{
				title: '知年喜舍介绍',
				id: 4,
				path: '/pages/me/introduce',
				icon: 'icon-right',
				size: '31rpx',
				isToken: false
			},
			{
				title: '在线客服',
				id: 5,
				path: '',
				icon: 'icon-right',
				size: '31rpx',
				isToken: false
			}
		],
		userinfo: { auth_name: '' },
		token: uni.getStorageSync('token'),
		friend_rule: 0,
		defaultImg: ''
	})
	let loginPopShow = ref(false)
	const getBanner = async () => {
		const res = await common.getBanner({ flag: 3 })
		if (res.code !== 200 || !res.data.length) return
		parames.banner = res.data[0]
	}
	const showToast = (title : any = '', icon : any = 'none') => {
		uni.showToast({
			title: title,
			icon: icon
		})
		loginPopShow.value = true
	}
	const jump = (url : string, isToken = true, id ?: number) => {
		if (!parames.token && isToken) return showToast('请登录')
		if (id === 5) return	//打开客服
		if(id === 3) {
			if(parames.userinfo.is_agent){//分销
				uni.navigateTo({
					url: '/pages/me/agent/index'
				})
			}else {//分销申请
				uni.navigateTo({
					url: '/pages/me/agent/application'
				})
			}
			return
		}
		uni.navigateTo({
			url: url
		})
	}
	const getMemberInfo = async (token ?: string) => {
		const res = await user.getMemberInfo()
		if (res.code === 401) {
			parames.userinfo = { auth_name: '' }
			parames.token = ''
		}
		if (res.code == 200) {
			parames.userinfo = res.data
			parames.token = uni.getStorageSync('token')
			uni.setStorageSync('userInfo', res.data)
			getBanner()
		}
	}
	const login = () => {
		if (parames.token) {
			uni.navigateTo({
				url: '/pages/me/personage'
			})
			return
		}
		loginPopShow.value = true
	}
	const close = () => {
		loginPopShow.value = false
	}
	const loginSuccess = (token : string) => {
		getMemberInfo(token)
	}
	const differenceTime = (time : any) => {
		// @ts-ignore
		let timestamp : any = (Date.parse(new Date())) / 1000; //计算当前时间戳
		return time < timestamp ? true : false
	}
	const makePhoneCall = (phoneNumber: string) => {
		uni.makePhoneCall({
			phoneNumber: phoneNumber,
			success: () => {
				console.log("拨打电话成功");
			},
			fail: (err) => {
				console.log("拨打电话失败:", err);
			}
		});
	}
	onShow(() => {
		parames.token = uni.getStorageSync('token')
		if (!uni.getStorageSync('token')) {
			parames.userinfo = { auth_name: '' }
			return
		}
		getMemberInfo()
	})
	onShareAppMessage(() => {
		return share()
	})
</script>

<template>
	<!-- <u-navbar :border-bottom="false" :is-back="false" title="" :background="{}" class="bg">
		<view class="slot-wrap f1">
			<view class="text flex flex-jc-c flex-ai-c">
				我的
			</view>
		</view>
	</u-navbar> -->
	<view style="position: relative;">
		<view style="position: absolute;bottom: 130rpx;color: #fff;z-index: 2;left: 30rpx;" class="flex" @click="login">
			<image :src="parames.userinfo.headavatar || parames.defaultImg"
				style="width: 100rpx;height: 100rpx;border-radius: 50%;" class="mr-20"></image>
			<view v-if="parames.token">
				<view class="font-32">{{parames.userinfo.nickname}}</view>
				<view class="u-font-xs">{{parames.userinfo.m_level}}</view>
				<view class="font-24" @click.stop="jump(`/pages/vip/index`, false)">
					<image src="../../static/image/vip.png" style="width: 141rpx;height: 18rpx;" mode="aspectFill">
					</image>
				</view>
			</view>
			<view class="font-32" v-else>请登录</view>
		</view>
		<view class="flex flex-jc-sb"
			style="position: absolute;z-index: 3;bottom: 0;width: calc(100% - 40rpx);height: 100rpx;left: 20rpx;"
			v-if="differenceTime(parames.userinfo.mlevel_pass_time || 0)" @click="jump(`/pages/vip/index`, false)">
			<image :src="parames.banner.content[0]" :style="{
				border: differenceTime(parames.userinfo.mlevel_pass_time || 0)? '1px solid #999999;' : ''
			}" style="width: 100%;height: 100%;border-top-right-radius: 15rpx;border-top-left-radius: 15rpx;" />
		</view>
		<!-- <view class="flex flex-jc-sb" style="position: absolute;z-index: 2;bottom: 0;color: #fff;;background: #217C2B;width: calc(100% - 40rpx);height: 100rpx;border-top-right-radius: 15rpx;border-top-left-radius: 15rpx;left: 20rpx;">
			<view style="padding: 20rpx 0 0 30rpx;">
				<view class="font-24">升级成为正式会员</view>
				<view class="font-20 mt-10">权益升级 享92折订房等更多权益</view>
			</view>
			<view @click="jump(`/pages/vip/index`, false)" style="background: #F6ED7B;color: #333;border-radius: 16rpx;height: 60rpx;margin: 20rpx 30rpx 0 0;padding: 0 20rpx;" class="flex flex-ai-c flex-jc-c">
				<view class="font-20 mr-20">￥<text class="font-bold font-32">199</text>/年</view>
				<view class="font-20">立即升级</view>
			</view>
		</view> -->
		<u-image src="https://zhinxs.com/image/bg.jpg" height="422"></u-image>
	</view>


	<view v-for="item in parames.list" :key="item.id" style="margin: 56rpx 0 30rpx 0;"
		@click="jump(item.path,item.isToken,item.id)">
		<view class="flex flex-ai-c flex-jc-sb grid" v-if="item.id != 5">
			<view>{{item.title}}</view>
			<view>
				<i-icon type="b" :icon="item.icon" :size="item.size" />
			</view>
		</view>
		<button open-type="contact" class="flex flex-ai-c flex-jc-sb grid" v-else>
			<view>{{item.title}}</view>
			<view>
				<i-icon type="b" :icon="item.icon" :size="item.size" />
			</view>
		</button>
	</view>

	<view class="u-text-center font-26" @click="makePhoneCall('15528214981')">
		<text style="color: #999999;">客服电话：</text>
		<text style="font-weight: bold;">15528214981</text>
	</view>
	<!-- 占位 -->
	<loginPop :show="loginPopShow" @close="close" @login="loginSuccess" />
	<view style="height: 200rpx;"></view>
	<tabbar :current="3" />
</template>
<style lang="scss">
	page {
		background: #fff;
		height: 100%;
	}
</style>
<style lang="scss" scoped>
	// 默认样式
	button {
		font-weight: 400;
		font-size: 28rpx;
		padding: 0;

		background: none;
	}

	// 边框样式
	button::after {
		border: 0;
	}

	// 点击时样式
	.button-hover {
		color: rgba(0, 0, 0, 0.6);
		background-color: #fff;
	}

	.bg {
		width: 100%;
		height: 422rpx;
		position: absolute;
		top: 0;
	}

	.grid {
		margin: 0 30rpx;
		height: 133rpx;
		background: #F7F7F7;
		padding: 0 60rpx 0 20rpx;
	}

	.slot-wrap {
		padding: 0 0 0 25rpx;

		.text {
			font-weight: 400;
			font-size: 32rpx;
			color: #fff;
		}

	}
</style>